#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <arpa/inet.h>
#include "socklib.h"

#define HOSTLEN  256
#define BACKLOG  1

static int make_server_socket_q(int portnum,int backlog)
{
	struct sockaddr_in saddr;
	struct hostent *hp;
	char hostname[HOSTLEN];
	int sock_id;
	
    printf("make_server_socket...\n");
	// 1.get a socket
	sock_id = socket(PF_INET,SOCK_STREAM,0);
	if (sock_id == -1){
        perror("socket");
		return -1;
	}
	
    printf("build address...\n");
	// 2.build address and bind it to socket
	bzero((void *)&saddr,sizeof(saddr));
	gethostname(hostname,HOSTLEN); 		// where am i?
	hp = gethostbyname(hostname); 		// get info about host
	//if (hp == NULL){
    //    perror("gethostbyname");
	//	return -1;
	//}
										// fill in post part
    printf("binding to socket...\n");
    if (hp != NULL) {
	    bcopy((void *)hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);
    }
	saddr.sin_port = htons(portnum); 	// fill in docket port
	saddr.sin_family = AF_INET; 		// fill in addr family
	if (bind(sock_id,(struct sockaddr *)&saddr,sizeof(saddr)) != 0){
        perror("bind");
		return -1;
	}
    printf("listen...\n");
	// 3.arrange for incoming calls
	if (listen(sock_id,backlog) != 0){
        perror("listen");
		return -1;
	}

	
	return sock_id;
}

int make_server_socket(int portnum)
{
	return make_server_socket_q(portnum,BACKLOG);
}

int connect_to_server(const char *host,int portnum)
{
	int sock;
	struct sockaddr_in servaddr;
	struct hostent *hp;

	// 1.get a socket
	if ((sock = socket(AF_INET,SOCK_STREAM,0)) == -1){
		perror("get a socket");
		return -1;
	}

	// 2.connect to server
	bzero((void *)&servaddr,sizeof(servaddr));
	hp = gethostbyname(host);
	if (hp == NULL){
		perror(host);
		return -1;
	}
	bcopy(hp->h_addr,(struct sockaddr *)&servaddr,hp->h_length);
	servaddr.sin_addr.s_addr = inet_addr(host);
	servaddr.sin_port = htons(portnum);
	servaddr.sin_family = AF_INET;

	if (connect(sock,(struct sockaddr *)&servaddr,sizeof(servaddr)) == -1){
		perror("connect to server");
		return -1;
	}

	return sock;
}

int sendMsg(int sockfd,const char *buf)
{
	int n = send(sockfd,buf,strlen(buf),0);
	if (n < 0){
		perror("send error");
	}
	return n;
}

int recvMsg(int sockfd,char *buf,int max_len)
{
	int n = recv(sockfd,buf,max_len,0);
	if (n < 0){
		perror("recv error");
	}
	return n;
}

